package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsCoupon;
import com.qf.pojo.DtsCouponUser;
import com.qf.service.DtsCouponService;
import com.qf.service.DtsCouponUserService;
import com.qf.util.AdminResponseUtil;
import com.qf.util.ResponseUtil;
import com.qf.vo.DtsCouponVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * projectName:java2017G5
 * author:hyn
 * time:2021/12/1 22:03
 * description:
 */
@CrossOrigin("*")
@RestController
@RequestMapping("/admin/coupon")
public class AdminCouponController {
    @Autowired
    private DtsCouponService dtsCouponService;

    @Autowired
    private DtsCouponUserService dtsCouponUserService;

    @RequestMapping("/list")
    public Object list( @RequestParam(name = "page" ,defaultValue = "1", required = false) Integer page ,
                        @RequestParam(name = "limit" ,defaultValue = "20", required = false) Integer limit ,
                        String name ,
                        Integer type ,
                        String status,
                        @RequestParam(name = "sort" ,defaultValue = "add_time", required = false)  String sort ,
                        @RequestParam(name = "order" ,defaultValue = "desc", required = false)  String order){
        IPage<DtsCoupon> page1 = dtsCouponService.findPage(page,limit,name,type,status,sort,order);
        HashMap<String, Object> data = new HashMap<>();
        data.put("total",page1.getTotal());
        data.put("items",page1.getRecords());
        return ResponseUtil.ok(data);
    }

    @RequestMapping("/read")
    public Object read(@RequestParam Integer id){
        DtsCoupon dtsCoupon = dtsCouponService.selectById(id);
        return ResponseUtil.ok(dtsCoupon);
    }

    @RequestMapping("/listuser")
    public Object listUser( @RequestParam(name = "page" ,defaultValue = "1", required = false) Integer page ,
                            @RequestParam(name = "limit" ,defaultValue = "20", required = false) Integer limit ,
                            @RequestParam Integer couponId ,
                            Integer userId ,
                            String status,
                            @RequestParam(name = "sort" ,defaultValue = "add_time", required = false)  String sort ,
                            @RequestParam(name = "order" ,defaultValue = "desc", required = false)  String order){
        IPage<DtsCouponVo> page1 = dtsCouponUserService.findUserPage(page,limit,couponId,userId,status,sort,order);
        HashMap<String, Object> data = new HashMap<>();
        data.put("total",page1.getTotal());
        data.put("items",page1.getRecords());
        return ResponseUtil.ok(data);
    }

    @PostMapping("/create")
    public Object create(@RequestBody DtsCoupon dtsCoupon){
        if (dtsCoupon == null) {
            return AdminResponseUtil.fail(-1,"优惠券不能为空");
        }
        DtsCoupon couponDb= dtsCouponService.findByName(dtsCoupon);
        if (couponDb!=null){
            return ResponseUtil.fail(-1,"该优惠券已存在");
        }
        if(dtsCoupon.getStartTime() != null && dtsCoupon.getEndTime() != null){
            Date date = new Date();
            if(dtsCoupon.getEndTime().compareTo(date) < 0 || dtsCoupon.getStartTime().compareTo(date) < 0){
                return ResponseUtil.fail(-1,"开始或结束时间不能小于当前时间");
            }


            if(dtsCoupon.getStartTime().compareTo(dtsCoupon.getEndTime()) > 0 ){
                return ResponseUtil.fail(-1,"结束时间不能小于开始时间");
            }
        }

        DtsCoupon coupon = dtsCouponService.insert(dtsCoupon);
        return ResponseUtil.ok(coupon);
    }


    @RequestMapping("/update")
    public Object update(@RequestBody DtsCoupon dtsCoupon){
        if (dtsCoupon == null) {
            return AdminResponseUtil.fail(-1,"优惠券不能为空");
        }
        DtsCoupon dtsCoupon1 = dtsCouponService.findById(dtsCoupon);
        if (!dtsCoupon.getName().equals(dtsCoupon1.getName())) {
            DtsCoupon couponDb = dtsCouponService.findByName(dtsCoupon);
            if (couponDb != null) {
                return ResponseUtil.fail(-1, "该优惠券已存在");
            }
        }
        if(dtsCoupon.getStartTime() != null && dtsCoupon.getEndTime() != null){
            Date date = new Date();
            if(dtsCoupon.getEndTime().compareTo(date) < 0 || dtsCoupon.getStartTime().compareTo(date) < 0){
                return ResponseUtil.fail(-1,"开始或结束时间不能小于当前时间");
            }


            if(dtsCoupon.getStartTime().compareTo(dtsCoupon.getEndTime()) > 0 ){
                return ResponseUtil.fail(-1,"结束时间不能小于开始时间");
            }
        }
        DtsCoupon coupon = dtsCouponService.update(dtsCoupon);
        return ResponseUtil.ok(coupon);
    }

    @RequestMapping("/delete")
    public Object delete(@RequestBody DtsCoupon dtsCoupon){
        List<DtsCouponUser> couponUsers= dtsCouponUserService.findByCouponId(dtsCoupon.getId());
        for (DtsCouponUser couponUser : couponUsers) {
            if (couponUser.getStatus()==1){
                return ResponseUtil.fail(-1,"该优惠券有用户持有");
            }
        }
        dtsCouponService.delete(dtsCoupon);
        return ResponseUtil.ok();
    }

}
